﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using LongSCADA_WPF.Helpers;
using LongSCADA_WPF.Message;
using LongSCADA_WPF.Models;
using LongSCADA_WPF.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace LongSCADA_WPF.ViewModels
{
    public partial class LoginViewModel:ObservableObject
    {
        private UserModelService UserModelServiceProp { get; }
        public LoginViewModel(UserModelService userModelService)
        {
            //通过Ioc 注入获取UserModelService
            UserModelServiceProp = userModelService;
        }

        [ObservableProperty]
        string? _userName = "visitor";

        [ObservableProperty]
        string? _passWord = "1111";

        [RelayCommand]
        void Login()
        {
            if (string.IsNullOrEmpty(_userName) || string.IsNullOrEmpty(_passWord))
            {
                UserModelServiceProp.ShowMessage("用户名或密码不能为空！");
                return;
            }
            //TODO: 登录逻辑
            List<UserModel> userList = SqlSugarHelper.Db.Queryable<UserModel>().Where(x => x.UserName == _userName && x.PassWord == _passWord).ToList();
            if (userList.Count > 0)
            {
                // UserModelServiceProp.ShowMessage("登录成功！");

                //TODO: 跳转到主界面
                UserModelServiceProp.CurrentUserModel = userList[0];
                //进行页面跳转到主页面，消息通知
                WeakReferenceMessenger.Default.Send(new LoginMessage(userList[0]));
            }
            else
            {
                UserModelServiceProp.ShowMessage("用户名或密码错误！");
            }
        }
    }
}
